package com.zjl.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjl.common.R;
import com.zjl.dto.DishDto;
import com.zjl.entity.Category;
import com.zjl.entity.Dish;
import com.zjl.entity.DishFlavor;
import com.zjl.service.CategoryService;
import com.zjl.service.DishFlavorService;
import com.zjl.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private  DishService dishService;
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private DishFlavorService dishFlavorService;

    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return R.success("保存成功");
    }

    /**
     * 菜品信息分页
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping(value = "page")
    public R<Page> page(Integer page,Integer pageSize,String name){
        //分页构造器
        Page<Dish> dishPage=new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Dish> dishWrapper=new LambdaQueryWrapper<Dish>();
        //过滤条件
        dishWrapper.like(name!=null,Dish::getName,name);
        dishWrapper.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);
        //进行查询
        dishService.page(dishPage,dishWrapper);

        //泛型为DishDto的分页构造器
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);

        //将dishPage对象中除了records以外的属性copy到dishDtoPage对象中，因为records是一个List<Dish>对象
        /*public static void copyProperties(@NotNull Object source,
                @NotNull Object target,
                String... ignoreProperties)*/
        BeanUtils.copyProperties(dishPage,dishDtoPage,"records");

        /**
         * 取出dishPage中的records，用其每个对象的categoryId字段值去查询category表中菜品分类名称
         * 新构造空的DishDto对象，将List<Dish>中的对象的属性值进行copy
         */
        List<Dish> records = dishPage.getRecords();
        List<DishDto> dishDtoRecords=new ArrayList<>();
        for (Dish record : records) {
            //获得菜品分类Id
            Long categoryId = record.getCategoryId();
            //获得菜品分类对象
            Category category = categoryService.getById(categoryId);
            //菜品分类对象存在时继续操作
            if (category!=null){
                //获取菜品分类名
                String categoryName=category.getName();
                //新建DishDto对象
                DishDto dishDto=new DishDto();
                //将Dish类型对象属性值copy给DishDto对象
                BeanUtils.copyProperties(record,dishDto);
                //为dishDto的CategoryName赋值
                dishDto.setCategoryName(categoryName);
                //dishDto完整对象加入dishDtoRecords
                dishDtoRecords.add(dishDto);
            }
        }

        //为dishDtoPage的Records赋值
        dishDtoPage.setRecords(dishDtoRecords);


        return R.success(dishDtoPage);
    }

    /**
     * 根据id查询菜品信息，并查询该菜品的口味信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<DishDto> getDishWithFlavors(@PathVariable Long id){
        DishDto dishDto = dishService.getByIdWithFlavor(id);
        return R.success(dishDto);
    }

    /**
     * 修改菜品信息
     * @param dishDto
     * @return
     */
    @PutMapping
    public R<String> updateWithFlavors(@RequestBody DishDto dishDto){
        dishService.updateWithFlavor(dishDto);
        return R.success("保存成功");
    }

    /**
     * //根据条件查询菜品
     * @param dish
     * @return
     */
    @GetMapping(value = "list")
    public R<List<DishDto>> list(Dish dish)
    {
        return R.success(dishService.getWithFlavors(dish));
    }
}
